package com.wunong.smart.algorithm.simple.structure;

import lombok.Data;

import java.util.LinkedList;
import java.util.List;

/**
 * @author created by zealot.zt
 */
@Data
public class Node<T> {

    /**
     * 数据
     */
    private T data;

    /**
     * 后继
     */
    private Node<T> next;

    public Node(T data) {
        this.data = data;
    }

    public Node(T data, Node<T> next) {
        this(data);
        this.next = next;
    }

    /**
     * 获取所有的数据
     *
     * @return
     */
    public List<T> toDataList() {
        Node<T> cur = this;

        // 结果列表
        List<T> result = new LinkedList<>();
        while (cur != null) {
            result.add(cur.getData());
            cur = cur.next;
        }

        return result;
    }

    /**
     * 获取数据数量
     *
     * @return
     */
    public int getSize() {
        int size = 0;
        Node<T> cur = this;

        while (cur != null) {
            size++;
            cur = cur.next;
        }

        return size;
    }

}
